#include "tcpclientsocket.h"

TcpClientSocket::TcpClientSocket(QObject *parent) : QTcpSocket(parent)
{
    connect(this, SIGNAL(readyRead()), this, SLOT(onDataRecved()));
    connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected())); // 连接断开信号
}

void TcpClientSocket::onDataRecved()
{
    while (bytesAvailable() > 0)
    {
        qint64 length = bytesAvailable();
        char buf[1024];
        read(buf, length);

        QString msg = buf;
        emit updateClients(msg, length); // 发出信号，向聊天室中的所有client广播
    }
}

void TcpClientSocket::onDisconnected()
{
    emit disconnected(this->socketDescriptor());
}
